package com.shop.item.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemBaseInput;
import com.shop.common.model.param.item.ItemDetailsInput;
import com.shop.common.model.result.item.ItemBaseOutput;
import com.shop.common.model.result.item.ItemDetailsOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.item.atom.atomsvc.IItemAtomService;
import com.shop.item.atom.dao.ItemBaseMapper;
import com.shop.item.atom.dao.ItemDetailsMapper;
import com.shop.item.api.model.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemAtomService extends BaseService implements IItemAtomService {
    @Autowired
    ItemBaseMapper itemBaseMapper;
    @Autowired
    ItemDetailsMapper itemDetailsMapper;

    @Override
    public ServiceBean<Integer> addItemBase(ItemBaseInput itemBase) {
        int num = itemBaseMapper.insertSelective(itemBase);
        if (opSuccess(num)) {
            return resultData(itemBase.getItemId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<Integer> addItemDetails(ItemDetailsInput itemDetails) {
        int num = itemDetailsMapper.insertSelective(itemDetails);
        if (opSuccess(num)) {
            return resultData(itemDetails.getId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteItemBase(ItemBaseInput itemBase) {
        int num = itemBaseMapper.deleteBySelective(itemBase);
        if (opSuccess(num)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteItemDetails(ItemDetailsInput itemDetails) {
        if (opSuccess(itemDetailsMapper.deleteBySelective(itemDetails))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<ItemBaseOutput> selectItemBase(ItemBaseInput itemBase) {
        ItemBaseOutput itemBaseOutput = itemBaseMapper.selectBySelective(itemBase).get(0);
        if (itemBaseOutput != null) {
            return resultData(itemBaseOutput);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<ItemDetailsOutput> selectItemDetails(ItemDetailsInput itemDetails) {
        ItemDetailsOutput detailsOutput = itemDetailsMapper.selectBySelective(itemDetails).get(0);
        if (detailsOutput != null) {
            return resultData(detailsOutput);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> updateItemBase(ItemBaseInput baseInput) {
        if (opSuccess(itemBaseMapper.updateByPrimaryKeySelective(baseInput))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> updateItemDetails(ItemDetailsInput detailsInput) {
        if (opSuccess(itemDetailsMapper.updateByPrimaryKeySelective(detailsInput))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<ItemBaseOutput>> selectItemBaseList(InputBean<ItemBaseInput> inputBean) {
        ItemBaseInput itemBaseInput = inputBean.getParam();
        List<ItemBaseOutput> list = itemBaseMapper.selectBySelective(itemBaseInput);
        if (list != null && list.size() > 0) {
            return resultData(list);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<Item>> selectItemList(InputBean<ItemBaseInput> inputBean) {
        ItemBaseInput itemBaseInput = inputBean.getParam();
        List<Item> list = itemBaseMapper.selectItemList(itemBaseInput);
        if (list != null && list.size() > 0) {
            return resultData(list);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<Map<String, Object>> selectItemList4P(InputBean<ItemBaseInput> inputBean) {
        if (inputBean.getIndex() == null || inputBean.getPageSize() == null) {
            return resultFailed();
        }
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<Item> itemList = itemBaseMapper.selectItemList(inputBean.getParam());
        PageInfo<Item> pageInfo = new PageInfo<>(itemList);
        Map<String, Object> map = new HashMap<>();
        if (pageInfo.getList() == null || pageInfo.getList().size() <= 0) {
            return resultFailed();
        }
        map.put("items", pageInfo.getList());
        map.put("count", pageInfo.getTotal());
        return resultData(map);
    }

    @Override
    public ServiceBean<String> mdfItemStatus(ItemBaseInput itemBaseInput) {
        if (opSuccess(itemBaseMapper.mdfItemStatus(itemBaseInput))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteItemBaseByIds(List<Integer> ids) {
        if (opSuccess(itemBaseMapper.deleteByIds(ids))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteItemDetailsByIds(List<Integer> ids) {
        if (opSuccess(itemDetailsMapper.deleteByIds(ids))) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<ItemDetailsOutput>> qryItemDetailList(ItemDetailsInput itemDetailsInput) {
        List<ItemDetailsOutput> list = itemDetailsMapper.selectBySelective(itemDetailsInput);
        return resultData(list);
    }


}
